公司地址:茂名市人民南路新村大院22號101
電話:13592986386
發(fā)布時間:2018/5/15 15:11:31
Bin 引用DLL
AppCode引用類文件
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
string userAgent = Request.UserAgent;
if (userAgent.ToLower().Contains("micromessenger"))
{
//Response.Write("歡迎您在微信中訪問我。");
//code=CODE&state=STATE
if (Request.QueryString["code"] != null)
{
//第一步:定義APPID等
string APPID = "*************************";
string APPSECRET = "*************************"; ;
//第二步:通過code換取網(wǎng)頁授權(quán)access_token
string code = Request.QueryString["code"];
ZSJson myjson = new ZSJson();
string url = "
string strReturn = myjson.GetFunction(url);
JObject jo = JObject.Parse(strReturn);
string ACCESS_TOKEN = jo["access_token"].ToString(); //讀取獲取到的ACCESS_TOKEN
string OPENID = jo["openid"].ToString(); //讀取獲取到的OPENID
Session["openid"] = OPENID;
//第四步:拉取用戶信息(需scope為 snsapi_userinfo)
string nexturl = "
string nextstrReturn = myjson.GetFunction(nexturl);
//Response.Write(nextstrReturn);
JObject result = JObject.Parse(nextstrReturn);
string nickname = result["nickname"].ToString();
string sex = result["sex"].ToString();
string headimgurl = result["headimgurl"].ToString();
string province = result["province"].ToString();
string city = result["city"].ToString();
/************業(yè)務邏輯(默認注冊帳號,注冊完成后進入某個指定頁面)****************************/
string Password = "123456"; //密碼
string cMD5Pass = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5"); //MD5加密
SqlConn mysql = new SqlConn();
Hashtable httParam = new Hashtable();
httParam.Add("@OPType", 777);
httParam.Add("@iRole", 1);
httParam.Add("@OPENID", OPENID);
httParam.Add("@cUserName", nickname);
httParam.Add("@cMobile", "");
httParam.Add("@cPic", headimgurl);
httParam.Add("@cPassword", cMD5Pass);
string StoredtName = "tb_User_Proc";
DataTable dt = mysql.ExecuteDataTable(StoredtName, httParam, CommandType.StoredProcedure);
if (dt.Rows.Count > 0)
{
CookieManage mycookies = new CookieManage();
mycookies.DeleteCookie("UserID");
mycookies.DeleteCookie("UserName");
mycookies.DeleteCookie("UserMobile");
int cookiestime = 1440;
mycookies.WriteCookie("UserID", dt.Rows[0]["ID"].ToString(), cookiestime, true);
mycookies.WriteCookie("UserName", dt.Rows[0]["cUserName"].ToString(), cookiestime, true);
mycookies.WriteCookie("UserMobile", dt.Rows[0]["cMobile"].ToString(), cookiestime, true);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language=javascript>autoPage()</script>");
}
/*************************************/
}
else
{
//第一步:用戶同意授權(quán),獲取code
//在確保微信公眾賬號擁有授權(quán)作用域(scope參數(shù))的權(quán)限的前提下(服務號獲得高級接口后,默認擁有scope參數(shù)中的snsapi_base和snsapi_userinfo),引導關注者打開如下頁面:
string APPID = "*************************";
string webdomain = "http://" + HttpContext.Current.Request.Url.Host;
string REDIRECT_URI = webdomain + "/Default.aspx";
string url = "
Response.Redirect(url);
}
}
else
{
//非微信瀏覽器打開(電腦,手機瀏覽器)
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language=javascript>autoPage()</script>");
}
}
}